package com.ys.erp.entity.data.bean;

import com.baomidou.mybatisplus.annotation.*;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.Getter;
import lombok.experimental.Accessors;

import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;

/**
 * @author lwj
 * @date 2023/4/16
 */
    
@Data
@TableName(value = "t_goods_specs")
@Accessors(chain = true)
public class GoodsSpecs {
    /**
     * 主键
     */
    @TableId(value = "id", type = IdType.INPUT)
    private Long id;

    /**
     * 商品id
     */
    @TableField(value = "goods_id")
    private Long goodsId;
    /**
     * sku图片
     */
    @TableField(value = "pic")
    private String pic;

    /**
     * json,规格
     */
    @TableField(value = "goods_specs")
    private String goodsSpecs;

    /**
     * 库存
     */
    @TableField(value = "stock")
    private Integer stock;

    /**
     * 规格价格（分）
     */
    @TableField(value = "specs_price")
    private Long specsPrice;

    @TableField(value = "delivery_cost")
    private Long deliveryCost;

    /**
     * 成本价格（分）
     */
    @TableField(value = "specs_cost_price")
    private Long specsCostPrice;

    @TableField(value = "jst_weight")
    private Integer jstWeight;
    
    /**
     * 是否删除0 否 1是
     */
    @TableField(value = "is_delete")
    private Integer isDelete;

    @TableField(value = "ys_coin_discount")
    private Integer ysCoinDiscount;

    /**
     * 重量
     */
    @TableField(value = "specs_weight")
    private Integer specsWeight;

    @TableField(value = "send_goods_type")
    private String sendGoodsType;

    @TableField(value = "version")
    @Version
    private Integer version;

    @TableField(value = "create_time")
    private LocalDateTime createTime;

    @TableField(value = "update_time", update = "now()")
    private LocalDateTime updateTime;

    @Data
    public static class CardGoodsSpecsDTO {
        private String key;

        private String value;

        private List<Goods.CardGoodsDTO> card;
    }

    /**
     * 计算幺币抵扣率
     * @param specs 规格集
     * @return 最大最小抵扣
     */
    public static Map<String, String> getYsCoinRange(List<GoodsSpecs> specs) {
        Optional<Integer> minCoinDiscount = specs.stream()
                .map(GoodsSpecs::getYsCoinDiscount)
                .min(Integer::compareTo);

        Optional<Integer> maxCoinDiscount = specs.stream()
                .map(GoodsSpecs::getYsCoinDiscount)
                .max(Integer::compareTo);
        HashMap<String, String> ysCoinDiscountMap = new HashMap<>();
        ysCoinDiscountMap.put("minYsCoinDiscount", minCoinDiscount.map(String::valueOf).orElse(""));
        ysCoinDiscountMap.put("maxYsCoinDiscount", maxCoinDiscount.map(String::valueOf).orElse(""));
        return ysCoinDiscountMap;
    }

    @AllArgsConstructor
    @Getter
    public enum Enums {

        NORMAL,
        ONLY_NO_SEND,
        ;
    }

    public static final Map<String, String> ENUM_MAP = new HashMap<>();

    static {
        ENUM_MAP.put(GoodsSpecs.Enums.NORMAL.toString(), "正常发货");
        ENUM_MAP.put(GoodsSpecs.Enums.ONLY_NO_SEND.toString(), "单拍不发");
    }

    public static boolean isContainsKey(String key) {
        return ENUM_MAP.containsKey(key);
    }

}